package com.smallkingson.ylib.net.http.bean;

import android.util.Pair;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.smallkingson.ylib.core.Yr;
import com.smallkingson.ylib.db.bean.KeyObtainable;
import com.smallkingson.ylib.db.cache.YCache;
import com.smallkingson.ylib.net.http.YHttps;

public class Response implements YCache,KeyObtainable {

	String cacheKey;
	String url;
	long time;
	long aliveSecond;
	String other;
	String result;
	public JSONObject jsonObject;
	public JSONArray jsonArray;
	public YHttps.From from;

	public String getCacheKey() {
		return cacheKey;
	}

	public void setCacheKey(String cacheKey) {
		this.cacheKey = cacheKey;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * 设置了这个就无需再设置setUrl(String url)与setCacheKey(String cacheKey)了
	 * 
	 * @param url
	 */
	public void yUrl(YUrl url) {
		this.url = url.getUrl();
		this.cacheKey = url.getNoAuthUrl();
	}

	public long getTime() {
		return time;
	}

	public void setTime(long time) {
		this.time = time;
	}

	public long getAliveSecond() {
		return aliveSecond;
	}

	public void setAliveSecond(long aliveSecond) {
		this.aliveSecond = aliveSecond;
	}

	public String getOther() {
		return other;
	}

	public void setOther(String other) {
		this.other = other;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {

		try {
			this.jsonObject = new JSONObject(result);
		} catch (Exception e) {
			if (e.getMessage() != null
					&& e.getMessage().contains(
							"JSONArray cannot be converted to JSONObject")) {
				try {
					this.jsonArray = new JSONArray(result);
				} catch (JSONException e1) {
					Yr.e(e1);
				}
			} else {
				Yr.e(e);
			}
		}

		this.result = result;
	}

	@Override
	public String toString() {
		return "Response [cacheKey=" + cacheKey + ", url=" + url + ", time="
				+ time + ", aliveSecond=" + aliveSecond + ", other=" + other
				+ ", result=" + result + "]";
	}

	@Override
	public long getKeepTime() {
		return this.aliveSecond;
	}

	@Override
	public long getCacheTime() {
		return this.time;
	}
	@Override
	public Pair<String, Object>[] obtainKey() {
		return new Pair[]{
				new Pair<>("cacheKey", this.getCacheKey())
		};
	}
}
